package cn.lbz.Food.Edit;

import cn.lbz.Food.Init.DBUtils;
import cn.lbz.Food.Init.DBfood;
import cn.lbz.Food.Init.Page;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serial;
import java.io.Writer;
import java.sql.SQLException;
import java.util.List;

public class EditFood extends HttpServlet {

    @Serial
    private static final long serialVersionUID = 1198763442711986380L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String sql = "SELECT * FROM share_food ORDER BY id DESC";
        try {
            List<DBfood> allfood = DBUtils.getfood(sql);
            try (Writer writer = response.getWriter()) {
                String sb = Page.getHeader() +
                        toHtmlTable(allfood) +
                        Page.getEND();
                writer.write(sb);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private String toHtmlTable(List<DBfood> books) {
        StringBuilder sb = new StringBuilder();
        sb.append("<center><br><table style='width:70%' border='0' cellpadding='10'>");

        sb.append("<tr style='background-color:#1d1b16;color:#cda45e'>" +
                "<th>编号</th><th>食物名称</th><th>地点</th><th>类型</th><th>价格</th><th>推荐原因</th><th>图片</th>" +
                "<th></th><th></th></tr>");
        for (int i=0; i<books.size(); i++) {
            DBfood allfood = books.get(i);
            if (i % 2 == 0) {
                sb.append("<tr style='background-color:#000000'>");
            } else {
                sb.append("<tr style='background-color:#1d1b16'>");
            }
            sb.append("<td>").append(allfood.getId()).append("</td>");
            sb.append("<td><b>").append(allfood.getName()).append("</b></td>");
            sb.append("<td>").append(allfood.getLocated()).append("</td>");
            sb.append("<td>").append(allfood.getType()).append("</td>");
            sb.append("<td>").append(allfood.getPrice()).append("</td>");
            sb.append("<td>").append(allfood.getreason()).append("</td>");
            sb.append("<td><img width='100px' src='../foodpics/").append(allfood.getPics().replaceAll(",", "")).append("'></img></td>");
            sb.append("<td>").append(
                    String.format("<a href='./updateget?id=%s'>修改</a>", allfood.getId())
            ).append("</td>");
            sb.append("<td>").append(
                    String.format("<a href='./deletefood?id=%s'>删除</a>", allfood.getId())
            ).append("</td>");
            sb.append("</tr>");
        }
        sb.append("</table></center>");
        return sb.toString();
    }

}

